package ecom.beans.dao;

import ecom.beans.ProductBean;
import ecom.beans.exception.ProductBeanException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class ProductDAO extends DAO {

    public static Long saveProduct(ProductBean product) throws ProductBeanException {

        if (product.getName().length() > 50) {
            throw new ProductBeanException("The name of the product should not exceed 50 characters");
        }
        if (product.getDescription() != null) {
            if (product.getDescription().length() > 500) {
                throw new ProductBeanException("The description of the product should not exceed 500 characters");
            }
        }
        if (product.getUrlPhoto() != null) {
            if (product.getUrlPhoto().length() > 100) {
                throw new ProductBeanException("The url photo of the product should not exceed 100 characters");
            }
        }
        saveOrUpdate(product);
        return product.getIdProduct();
    }

    public static void deleteProduct(ProductBean product) {
        delete(product);
    }

    public static void updateProduct(ProductBean product) {
        saveOrUpdate(product);
    }

    public static Set getAllProducts(String order) {
        return fetchAll("ProductBean.findAll", null, order);
    }

    public static ProductBean getByIdProduct(Long idProduct) {
        List parameters = new ArrayList();
        parameters.add("idProduct");
        parameters.add(idProduct);
        return (ProductBean) fetchOne("ProductBean.findByIdProduct", parameters);
    }

    public static Set<ProductBean> getByNameOrDesciption(String name, String description, String order) {
        List parameters = new ArrayList();
        parameters.add("name");
        parameters.add("%" + name + "%");
        parameters.add("description");
        parameters.add("%" + description + "%");
        return fetchAll("ProductBean.findByNameOrDescription", parameters, order);
    }

    public static Set<ProductBean> getByIdProductcategory(Long idProductcategory, String order) {
        List parameters = new ArrayList();
        parameters.add("idProductcategory");
        parameters.add(idProductcategory);
        return fetchAll("ProductBean.findByIdProductcategory", parameters, order);
    }

    public static Set<ProductBean> getByIdProductstore(Long idProductstore, String order) {
        List parameters = new ArrayList();
        parameters.add("idProductstore");
        parameters.add(idProductstore);
        return fetchAll("ProductBean.findByIdProductstore", parameters, order);
    }

    public static Set<ProductBean> getByPriceInterval(Double firstPrice, Double secondPrice, String order) {
        List parameters = new ArrayList();
        parameters.add("firstPrice");
        parameters.add(firstPrice);
        parameters.add("secondPrice");
        parameters.add(secondPrice);
        return fetchAll("ProductBean.findByPriceInterval", parameters, order);
    }

    public static Set<ProductBean> getProductsByByMaximumPrice(Double maxPrice, String order) {
        List parameters = new ArrayList();
        parameters.add("maxPrice");
        parameters.add(maxPrice);
        return fetchAll("ProductBean.findByMaximumPrice", parameters, order);
    }
}
